﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Interop;

namespace InfonavaEye.util
{
    public class StraightHelper
    {
        //static HDevelopExport.HDevelopExport tool = new HDevelopExport.HDevelopExport();
        static ImageProcessing.ImageProcessing tool = new ImageProcessing.ImageProcessing();

        public static string straight(string filePath,int iRotateAngle =0) {
            try
            {
                ImageProcessing.ImageProcessing.InputParametersFilePath input_data;
                input_data.input_dir = filePath;        //输入图像路径或文件夹路径
                input_data.iMeasureAmplitude = 10;   //圆弧卡尺幅宽
                input_data.iMeasureSigmea = 5;      //圆弧卡尺高斯滤波参数
                input_data.iRotateAngle = iRotateAngle; //裁剪后图片旋转角度
                Stopwatch sw = new Stopwatch();
                sw.Start();
                string mesg = tool.CropPolarImageFilePath(input_data);
                sw.Stop();
                if (mesg == "true")
                {
                    return filePath.Replace(".jpg", "_1.jpg");
                }
                else
                {
                    switch (mesg)
                    {
                        case "Error1":
                            //return "没找到圆";
                            return "无钉";
                        case "Error2":
                            return "切割";
                        case "Error3":
                            return "无钉";
                        default:
                            return mesg;
                    }
                }

            }
            catch (Exception ex)
            {
                Console.WriteLine("拉直算法发生异常：{0}，{1}", ex.Message, ex.StackTrace);
                // return "发生异常:"+ex.Message;
                return "图片处理失败";
            }


        }

        public static string Straight2(string filePath)
        {
            /*
             Bitmap inputBitmap = null;
             inputBitmap = (Bitmap)Bitmap.FromFile(filePath, false);
             Rectangle rect = new Rectangle(0, 0, inputBitmap.Width, inputBitmap.Height);
             BitmapData bitmapData = inputBitmap.LockBits(rect, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
             IntPtr pImageData = bitmapData.Scan0;

             tool.crop_image(pImageData, inputBitmap.Width, inputBitmap.Height, 3, "bgr", filePath,
                             out IntPtr pOutImageData, out int iOutImageWidth, out int iOutImageHeight);
             bitmapData = null;
            inputBitmap.Dispose();
             */
            try {
                // string mesg = tool.CropPolarImageFilePath(filePath);
                string mesg = "";
                Console.WriteLine("拉直算法返回信息：", mesg);
                if (mesg == "true")
                {
                    return filePath.Replace(".jpg", "_1.jpg");
                }
                else
                {
                    switch (mesg) {
                        case "Error1":
                            //return "没找到圆";
                            return "无钉";
                        case "Error2":
                            return "切割";
                        case "Error3":
                            return "无钉";
                        default:
                         return  mesg;
                    }
                }
            } catch (Exception ex) {
                Console.WriteLine("拉直算法发生异常：{0}，{1}", ex.Message,ex.StackTrace);
                // return "发生异常:"+ex.Message;
                return "图片处理失败";
            }
        }

        public static string Straight(string filePath)
        {
            try
            {
                //1. 初始化套接字
                Socket ClientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                //2. 连接服务器
                IPAddress ip = IPAddress.Parse("127.0.0.1");
                int port = 8869;
                ClientSocket.Connect(new IPEndPoint(ip, port));
                Console.WriteLine("成功与8869端口建立连接,{0}", DateTime.Now.ToString("HH:mm:ss:fff"));

                //发送数据
                byte[] buffer = Encoding.UTF8.GetBytes(filePath);
                ClientSocket.Send(buffer);

                //接收结果
                byte[] receiveBuffer = new byte[1024];
                int counts = ClientSocket.Receive(receiveBuffer);
                if (counts > 0)
                {
                    var result = System.Text.Encoding.GetEncoding("GB2312").GetString(receiveBuffer, 0, counts);
                    ClientSocket.Close();
                    Console.WriteLine("收到数据 {0}, {1}", DateTime.Now.ToString("HH:mm:ss:fff"), result);
                    if (result != null && result.IndexOf('|') > -1)
                    {
                        return result.Split('|')[1];
                    }
                }
                return null;
            }
            catch (Exception)
            {
                MessageBox.Show("连接Python拉直程序失败，请检查是否启动了Python拉直程序的服务器", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                //throw;
                return null;
            }
        }
    }
}
